//axiosInstance.js
//导入axios
import axios from 'axios'
import { message } from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
import router from '../router';

const service = axios.create({
    baseURL: "http://8.130.78.116:8080/",
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    timeout: 6000 // 指定请求超时的毫秒数(0 表示无超时时间)
})

//写入请求头
service.interceptors.request.use(config => {
    config.headers["token"] = window.localStorage.getItem('token');
    return config
})


//拦截异常code
service.interceptors.response.use(response => {
    //请求server成功
    //console.log(response)
    let res = response.data

    switch (res.resCode) {
        case 200: {
            //message.success(res.msg);
            return response
        }
        case 401: {
            message.error(res ? res.msg : 'token无效')
            router.push('/login')
            return Promise.reject(response.data.msg)
        }
        default: {
            message.error(res ? res.msg : '获取数据异常')
            return Promise.reject(response.data.msg)
        }
    }
}, error => {
    //请求server失败
    if (error.message.data) {
        error.message = error.response.msg
    }
    message.error(error.message)
    return Promise.reject(error)
})

export default service